#include <string>
#include <map>
#include <numeric>
#include <functional>
#include <iostream>
#include <set>

struct plus2nd {
    template <typename T1, typename T2>
    inline T1 operator()(T1 t1, const T2 &t2) const {
        return t1 + t2.second;
    }
};

template <typename Map, typename Key, typename Value>
Value accumulate_range(const Map & map, const Key & key, Value init)
{
    typedef typename Map::const_iterator const_iterator;
    typedef typename Map::value_type value_type;
    // 得到所有键值位key的数据，构造成一个迭代器
    std::pair<const_iterator, const_iterator> range = map.equal_range(key);
    // 输入键值为key的迭代器的开始和末尾，得到键值为key的所有值的和
    return std::accumulate(range.first, range.second, init, plus2nd());
}

// int main()
// {
//     std::multimap<std::string, int> mymap;
//     std::set<std::string> keys;
//     mymap.insert(std::multimap<std::string, int>::value_type("A", 1));
//     mymap.insert(std::multimap<std::string, int>::value_type("B", 3));
//     mymap.insert(std::multimap<std::string, int>::value_type("A", 4));
//     mymap.insert(std::multimap<std::string, int>::value_type("B", 4));
//     mymap.insert(std::multimap<std::string, int>::value_type("A", 4));
//     mymap.insert(std::multimap<std::string, int>::value_type("B", 4));
//     mymap.insert(std::multimap<std::string, int>::value_type("C", 4));
//     for (const auto &i : mymap) {
//         keys.insert(i.first);
//     }
//     for (const auto &i : keys) {
//         std::cout << "Sum of "<< i <<": " << accumulate_range(mymap, i, 0) << std::endl;
// }